﻿var log = {};
log.timeout =1000;
log.loading_text = lang.loading;
$(function () {
    log.init();
    $(".hu8-log-button").live("click", function () {
        $(".hu8-log").show();
        $(this).hide();
    });
    $(".hu8-log-close").live("click", function () {
        $(".hu8-log").hide();
        $(".hu8-log-button").show();
    });
    $(".hu8-log-result-close").live("click", function () {
        $(".hu8-log-result-container").html("");
        $(".hu8-log-result").hide();
    });
});

//初始化log
log.init = function () {
//    var html = "<div class='hu8-log-button'></div><div class='hu8-log'><div class='hu8-log-header'><strong>" + lang.operation_log + "</strong><div class='hu8-log-close'></div></div><div class='hu8-log-container'><ul class='hu8-log-list'></ul></div></div>";
//    html += '<div class="hu8-log-result" style="display:none"><div class="hu8-log-result-close"></div><div class="hu8-log-result-container"></div></div>';
//    html += "<div class='hu8-loading'>" + log.loading_text + "</div>";
//    html += "<style>";
//    html += "</style>";
//    $("body").append(html);
    log.ajax_setup();
}
//log
log.log_text = function (text, is_error) {
    var html = "<li class=''>[" + (new Date()).toTimeString() + "]: " + text + "</li>";
    $(".hu8-log-list").append(html);
    $(".hu8-log-result-container").html(text);
    $(".hu8-log-result").show();
    setTimeout(log.close_waiting_box, log.timeout);
}
log.close_waiting_box = function () {
    $(".hu8-log-result").hide("fade");
}

log.hide = function () {
    $(".hu8-log-button").hide();
    $(".hu8-log").hide();
}
log.show = function () {
    $(".hu8-log-button").show();
    $(".hu8-log").show();
}

window.onerror = function (e) {
    log.log_text("Javascript" + lang.error + "： " + e, true);
    $(".hu8-loading").hide();
};

//AJAX通用库
log.ajax_setup = function () {
    //error catching
    $.ajaxSetup({
        error: function (x, e) {
            if (x.status == 0) {
                log.log_text(lang.error_offline, true);
            }
            else {
                if (x.status == 404) {
                    log.log_text(lang.error_404, true);
                }
                else {
                    if (x.status == 500) {
                        log.log_text(lang.error_server +x.responseText, true);
                    }
                    else {
                        if (e == 'parsererror') {
                            log.log_text(lang.error_parse, true);
                        }
                        else {
                            if (e == 'timeout') {
                                log.log_text(lang.error_timeout, true);
                            }
                            else {
                                log.log_text(lang.unknown + x.responseText, true);
                            }
                        }
                    }
                }
            }
        }
    });
    //loading status
    $(".hu8-loading").ajaxStart(function () {
        $(this).show("fade");
    });
    $(".hu8-loading").ajaxStop(function () {
        $(this).hide("fade");
    });
}